package com.extension.company.view.webview;

import android.annotation.SuppressLint;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.extension.company.admin.R;
import com.extension.company.common.mvc.BaseTitleActivity;
import com.extension.company.common.utils.StringUtils;

public class WebViewActivity extends BaseTitleActivity implements View.OnClickListener {

	private WebView mWebView;
	private ProgressBar pb;
	private String title;
	private String url = "";

	public static void startActivity(Context activity, String title, String url) {
		Intent intent = new Intent(activity, WebViewActivity.class);
		intent.putExtra("title", title);
		intent.putExtra("url", url);
		activity.startActivity(intent);
	}
	
	@Override
	public int getMainLayout() {
		// TODO Auto-generated method stub
		return R.layout.webview;
	}

	@Override
	public void initView() {
		title = getIntent().getStringExtra("title");
		setTitle(title);
		updateSuccessView();
		mWebView = (WebView) findViewById(R.id.mWebView);
		pb = (ProgressBar) findViewById(R.id.pb);
    }
	
	@SuppressLint({ "JavascriptInterface", "NewApi" })
	@Override
	public void initData() {
		url = getIntent().getStringExtra("url");
		mWebView.setHorizontalScrollBarEnabled(false);//水平ScrollBar
		mWebView.setVerticalScrollBarEnabled(true);   //垂直ScrollBar
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setDomStorageEnabled(true);
		mWebView.getSettings().setSupportZoom(true);          // 启用页面的缩放
		mWebView.getSettings().setBuiltInZoomControls(true);  // 启用页面缩放的按钮
		mWebView.getSettings().setDisplayZoomControls(false); // 隐藏页面缩放的按钮
		mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		
		mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setLoadWithOverviewMode(true); 
		mWebView.loadUrl(url);

		mWebView.addJavascriptInterface(this, "imagelistner");
		setWebViewClientListener();
	}

	@Override
	public void initListener() {

	}

	private void setWebViewClientListener() {
		mWebView.setWebViewClient(new WebViewClient(){
			 
		      @Override
		      public boolean shouldOverrideUrlLoading(WebView view, String url) {
		        // TODO Auto-generated method stub
		    	if (StringUtils.CheckUrl(url)) {
		    		view.loadUrl(url);
				}
		        return true;
		      }
		      
		      @Override
		      public void onPageStarted(WebView view, String url, Bitmap favicon) {
		    	  
		    	  if (StringUtils.CheckUrl(url)) {
		    		  pb.setVisibility(View.VISIBLE);
			    	  mWebView.setWebChromeClient(new WebChromeClient());
				   }
		      }
		   });
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.m_back:
				finish();
				break;
			default:
				break;

		}
	}


	public class WebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
        	pb.setProgress(newProgress);  
            if(newProgress >= 100){  
                pb.setVisibility(View.GONE);  
            } 
            super.onProgressChanged(view, newProgress);
        }
     }

//	 @Override
//	 public void onBtnCancel() {
//		 if (mWebView.canGoBack()) {
//			 mWebView.goBack();
//		}else {
//			super.onBtnCancel();
//		}
//	 }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mWebView.clearHistory();
		mWebView.removeAllViews();
		mWebView.destroy();
		mWebView = null;
	}
}






